<?php
// $Id: editview.theme.inc,v 1.1.2.2 2009/05/04 13:28:02 agileware Exp $

/**
 * @file
 *
 */

/**
 * Prepare variables for the editview row template.
 *
 * @param $variables
 *   An array of variables from the theme system.
 */
function template_preprocess_editview_view_row_location_edit(&$variables) {

  static $run = TRUE;
  if ($run) {
    drupal_add_js('misc/jquery.form.js');
    drupal_add_js(drupal_get_path('module', 'editview_location') .'/theme/editview.js');
    drupal_add_js(array('editviewLocation' => array('submit_url' => url('editview_location_js'))), 'setting');
    drupal_add_css(drupal_get_path('module', 'editview_location') .'/theme/editview.css');
    module_load_include('inc', 'node', 'node.pages');
    $run = FALSE;
  }

  $lid = $variables['row']->lid;
  $location = array('lid' => $lid);

  // Add the node form to the variables array.
  $variables['form'] = drupal_get_form(
    'editview_location_form_'. $lid,
    $location,
    $variables['view'],
    $variables['id'],
    request_uri(),
    array('form' => array('fields' => $variables['options'])));
}

/**
 * Display selected fields from the node form in a table.
 *
 * @param $form
 *   A node form array.
 * @return
 *   An HTML form.
 * @ingroup themeable
 */
function theme_editview_location_form($form) {
  $view = $form['#parameters'][3];
  $header = array();
  $row = array();
  // Add selected fields to table as form fields.
  foreach ($view->field as $field) {
    // If auto_nodetitle module is enabled and the title field is to be overridden don't display it.
    if (module_exists('auto_nodetitle') && $field->real_field == 'title') {
      if (auto_nodetitle_get_setting($form['#node']->type) == AUTO_NODETITLE_ENABLED) {
        continue;
      }
    }

    $header[] = $field->label();
    $classes = array();
    $cell = array('data' => '');
    foreach (module_implements('field_form_render') as $module) {
      $function = $module .'_field_form_render';
      $result = $function($form, $field);
      foreach ($result as $key => $value) {
        switch ($key) {
          case 'data':
            $cell['data'] .= $value;
            break;
          case 'class':
            $classes[] = $value;
            break;
          default:
            $cell[$key] = $value;
            break;
        }
      }
    }
    $cell['class'] = implode(' ', $classes);
    $row[] = $cell;
  }
  $buttons = drupal_render($form['buttons']['submit']);
  $buttons .= drupal_render($form['buttons']['delete']) .'<span style="display: none;">'. drupal_render($form) .'</span>'."\n";
  $handler = $view->style_plugin;
  $active = !empty($handler->active) ? $handler->active : '';
  $order = !empty($handler->order) ? $handler->order : 'asc';
  return theme('table', $header, array($row), array('class' => 'editview-row')) . $buttons;
}